package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.entity.WlwsysProject;
import com.ruoyi.common.utils.random.RandomUtil;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.wlwsys.WlwsysProjectMapper;
import com.ruoyi.system.service.WlwsysProjectService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


/**
* @author 86187
* @description 针对表【wlwsys_project】的数据库操作Service实现
* @createDate 2023-07-19 17:34:30
*/
@Slf4j
@Service
public class WlwsysProjectServiceImpl extends ServiceImpl<WlwsysProjectMapper, WlwsysProject>
    implements WlwsysProjectService {
    @Autowired
    private WlwsysProjectMapper wlwsysProjectMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    //分页查询
    @Override
    public List<WlwsysProject> getProjectByPageInfo(Integer pageNum, Integer pageSize, String projectId, String projectName, Long userID,String startTime,String endTime) throws ParseException {
        PageHelper.startPage(pageNum,pageSize);
        if (StringUtils.isBlank(projectId)) {
            projectId="";
        }
        if (StringUtils.isBlank(projectName) ) {
            projectName="";
        }
        String userid;
        if (ObjectUtils.isEmpty(userID) ) {
            userid="";
        }else{
            userid=String.valueOf(userID);
        }
        if(StringUtils.isBlank(startTime)){
            System.out.println("没有时间的分页查询");
            return wlwsysProjectMapper.selectProject(projectId,projectName,userid);
        }
        startTime = startTime.replace("Z", " UTC");
        endTime = endTime.replace("Z", " UTC");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
        SimpleDateFormat chinaSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        startTime = chinaSdf.format(sdf.parse(startTime));
        endTime = chinaSdf.format(sdf.parse(endTime));
        return wlwsysProjectMapper.selectProjectHaveTime(projectId,projectName,userid,startTime,endTime);
    }

    //删除数据通过项目ID
    @Override
    public void deleteByProjectId(String projectID) {
        wlwsysProjectMapper.deleteByProjectId(projectID);
    }

    //新增一个项目
    @Override
    public void insertProject(String projectName, String projectIntroduce,String userName) throws ParseException {
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String time = dateFormat.format(date);
        SysUser sysUser = sysUserMapper.selectUserByUserName(userName);
        String projectId = "p_" + RandomUtil.getRandomNumber(8);
        if(projectName==null){
            projectName="";
        }
        if(projectIntroduce==null){
            projectIntroduce="";
        }
        wlwsysProjectMapper.insertNew(projectId,projectName,projectIntroduce,sysUser.getUserId(),time);
    }

    //更新项目
    @Override
    public boolean UpdateProject(String projectId, String projectName, String projectIntroduce) throws ParseException {
        //获取当前的时间
        Date date1 = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String time = dateFormat.format(date1);
        if(projectName==null){
            projectName="";
        }
        if(projectIntroduce==null){
            projectIntroduce="";
        }
        return wlwsysProjectMapper.updateByProjectId(projectId,projectName,projectIntroduce,time);
    }

    //获取指定用户下的所有项目ID
    @Override
    public List<String> selectProjectIdByUserId(Long userId) throws ParseException {
        return wlwsysProjectMapper.selectProjectIdByUserIdStrings(userId);
    }

    //获取项目ID对应的用户ID
    @Override
    public Long selectUserIdByProjectId(String projectId) throws ParseException {
        return wlwsysProjectMapper.selectUserIDbyProjectId(projectId);
    }
}




